package dataStructrue.squence;

import java.util.Arrays;

public class SquenceList<T> {
    private T[] eles;
    private int length;
    // 初始化
    public SquenceList(int capacity) {
        eles = (T[]) new Object[capacity];
        this.length = 0;
    }
    // 置空
    public void clear() {
        this.length = 0;
    }
    // 判空
    public boolean isEmpty() {
        return this.length == 0;
    }
    // 获取当前元素个数
    public int length() {
        return this.length;
    }
    // 获取指定元素
    public T get(int index) {
        return this.eles[index];
    }
    // 插入元素
    public void insert(T ele,int index) {
        if (index>this.length||index<0) {
            System.out.println("错误");
            return;
        }
        for (int i = this.length-1; i >=index ; i--) {
            eles[i+1] = eles[i];
        }
        this.length++;
        eles[index] = ele;
    }
    // 删除元素
    public T delete(int index) {
        if (index>this.length||index<0) {
            System.out.println("错误");
            return null;
        }
        T temp = eles[index];
        for (int i = index; i <length ; i++) {
            eles[i] = eles[i+1];
        }
        this.length--;
        return temp;
    }
    // 输出线性表中的元素
    public void display() {
        System.out.println(Arrays.toString(this.eles));
    }

    public static void main(String[] args) {
        SquenceList<Integer> squenceList = new SquenceList<Integer>(50);
        squenceList.display();
        System.out.println(squenceList.length());
        squenceList.insert(100,0);
        System.out.println(squenceList.length());
    }
}
